import { app, shell, BrowserWindow, ipcMain, globalShortcut, dialog,screen } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { readDir } from './readDir'
import { getElectronVersion } from './version'
import createKoaApp from './server'
function createWindow() { 
    const { width, height } = screen.getPrimaryDisplay().workAreaSize;
    createKoaApp()
    console.log(width, height ,is.dev)
    const mainWindow = new BrowserWindow({
        width: width,
        height: height,
        show: false,
        autoHideMenuBar: true,
        icon,
        webPreferences: {
            preload: join(__dirname, '../preload/index.js'),
            sandbox: false, 
            webSecurity: false, 
            devTools: is.dev ? true : false,
            contextIsolation: false,
            nodeIntegration: true,
            nodeIntegrationInWorker: true
        } 

    })

    mainWindow.on('ready-to-show', () => {
        mainWindow.show() 
    })

    mainWindow.webContents.setWindowOpenHandler((details) => {
        shell.openExternal(details.url)
        return { action: 'deny' }
    }) 
    if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
        mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
    } else {
        mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
    }

    // 设置DevTools快捷键
    globalShortcut.register('CommandOrControl+Shift+i', function () {
        mainWindow.webContents.openDevTools()
    })

    return mainWindow
}

// 程序单例模式
let myWindow = null
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
    app.quit()
} else {
    app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
        if (myWindow) {
            dialog.showMessageBox({ message: '此程序已经正在运行' }) 
            if (myWindow.isMinimized()) myWindow.restore() 
            myWindow.focus()
        }
    }) 
    app.whenReady().then(() => { 
        electronApp.setAppUserModelId('com.electron') 
        app.on('browser-window-created', (_, window) => {
            optimizer.watchWindowShortcuts(window)
        })

        myWindow = createWindow()

        app.on('activate', function () { 
            if (BrowserWindow.getAllWindows().length === 0) createWindow()
        })
    })
 
    app.on('window-all-closed', () => {
        if (process.platform !== 'darwin') {
            app.quit()
        }
    })
}

ipcMain.on('readDir', readDir)
ipcMain.handle('getElectronVersion', getElectronVersion)
